package com.baosight.sczx.zm.cl.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.User;
import com.baosight.sczx.common.zm.cInterface.cc.Qualitystandard;
import com.baosight.sczx.common.zm.cInterface.cl.RiskRecord;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.zm.cl.domain.ZMCLReport_RiskRecord;

/**
 * 安全日报(数据维护)
 * 从表,隐患记录
 * <p>Title: ServiceZMCLR2.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 严洪浩
 * @version 1.0
 * <pre>Histroy:
 *          2011-11-18 上午08:50:39  严洪浩  Create
 *</pre>
 */
public class ServiceZMCLR2 extends ServiceEPBase {
	
	/**
	 * 
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCLR2</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-11-18上午08:50:47 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo)
	{
		EiInfo outInfo;
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		outInfo = this.query(inInfo);
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cl.riskSource"));//隐患来源
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cl.riskClassify"));//隐患分类
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cl.riskLevel"));//隐患级别
		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cl.department", "orgCode", "orgName", false));//隐患单位
		outInfo.addBlock(User.getInstance().getConstructionStaff("cmes.cl.empName", "empCode", "empName", true));
		return outInfo;
	}
	
	/**
	 * 查询隐患报表下隐患项目方法
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *     2011-6-2  高强
	 * </p>
	 *
	 */
	public EiInfo query(EiInfo inInfo) {
		StaticTools.setQueryParameters(inInfo, "fdReportId", inInfo.getString("fdReportId"), true);//添加查询条件 主表ID
		StaticTools.setQueryParameters(inInfo, "fdType", inInfo.getString("fdType"), true);//添加查询条件 隐患类型
		EiInfo outInfo = super.query(inInfo, "ZMCLReport_RiskRecord.query", new ZMCLReport_RiskRecord());
		return outInfo;
	}
	
	/**
	 * 报表查询所有的记录方法
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *     2011-6-2  高强
	 * </p>
	 *
	 */
	public EiInfo queryAll(EiInfo inInfo) {
		StaticTools.setQueryParameters(inInfo, "fdReportId", inInfo.getString("fdReportId"), true);//添加查询条件
		inInfo.addBlock("result").set(EiConstant.offsetStr, 0);
		inInfo.getBlock("result").set(EiConstant.limitStr, 1000);
		inInfo.getBlock("result").set(EiConstant.isCountFlag, true);
		EiInfo outInfo = super.query(inInfo, "ZMCLReport_RiskRecord.query", new ZMCLReport_RiskRecord(), false, null, EiConstant.queryBlock, "result", "result");
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cl.riskSource"));//隐患来源
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cl.riskClassify"));//隐患分类
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cl.riskLevel"));//隐患级别
		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cl.department", "orgCode", "orgName", false));//隐患单位
		outInfo.set("count", getCivilizationProduceCount(inInfo.getString("fdReportId").toString()));
		return outInfo;
	}
	
	/**
	 * 符合日期条件的隐患记录查询
	 * <p>Function: queryReportNum</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *     2011-5-30              高强       
	 * </p>
	 *
	 */
	public EiInfo queryReportNum(EiInfo inInfo)
	{
		EiInfo outInfo = RiskRecord.getRiskRecordInfo(inInfo);
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cl.riskSource"));//隐患来源
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cl.riskClassify"));//隐患分类
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cl.riskLevel"));//隐患级别
		outInfo.addBlock(Qualitystandard.getQualityStandardItems("cmes.cl.quality", "在用", true));//质量标准化分类
		return inInfo;
	}
	
	/** 新增隐患项目
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-5-30  高强
	 * </p>
	 *
	*/
	@Override
	public EiInfo insert(EiInfo inInfo) {
		EiBlock resultBlock = inInfo.getBlock(EiConstant.resultBlock);
		String fdType = inInfo.getBlock("inqu_status").getCellStr(0, "fdType").toString();
		String fdReportId = inInfo.getString("fdReportId");//主表ID
		String fdRiskRecordIds = "";
		
		for(int i = 0; i < resultBlock.getRowCount(); i++)
		{
			String fdRiskRecordId = inInfo.getCellStr("result", i, "fdKeyId");
			
			resultBlock.setCell(i, "fdKeyId", TablePk.getInstance().getNextValue("T_CL_REPORT_RISKRECORD"));
			resultBlock.setCell(i, "fdRiskRecordId", fdRiskRecordId);//隐患记录ID
			resultBlock.setCell(i, "fdReportId", fdReportId);
			resultBlock.setCell(i, "fdReportPeopleId", UserSession.getUserId());
			resultBlock.setCell(i, "fdReportPeople", UserSession.getInSessionProperty("UserCname"));
			resultBlock.setCell(i, "fdReportDepartmentId", UserSession.getInSessionProperty("orgCode"));
			resultBlock.setCell(i, "fdReportDepartment", UserSession.getInSessionProperty("orgName"));
			resultBlock.setCell(i, "fdReportTime", StaticTools.getDateTime());
			resultBlock.setCell(i, "fdDeadline", inInfo.getBlock("result").getCellStr(i, "fdDeadlineDate"));//解决日期
			resultBlock.setCell(i, "fdType", fdType.equals("安全隐患")?"安全隐患":"工程质量及文明生产");
			
			if(i < (resultBlock.getRowCount() - 1)) {
				fdRiskRecordIds += "'"+fdRiskRecordId+"',";
			}
			else {
				fdRiskRecordIds += "'"+fdRiskRecordId+"'";
			}
		}
		
		if(resultBlock.getRowCount() > 0) {
			Map<String, String> args = new HashMap<String, String>();
			args.put("fdReportId", fdReportId);
			args.put("fdRiskRecordIds", fdRiskRecordIds);
			boolean validate = this.validate(inInfo, args);
			if(validate) {
				return inInfo;
			}
			EiInfo outInfo = super.update(inInfo,"ZMCLReport_RiskRecord.insert");
			return outInfo;
		}
		else {
			inInfo.setStatus(-1);
			inInfo.setMsg("没有选择隐患记录！");
			return inInfo;
		}
	}
	
	/**
	 * 删除报表下的隐患项目方法
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *      2011-6-2  高强
	 * </p>
	 *
	 */
	
	public EiInfo delete(EiInfo inInfo) 
	{
		EiInfo _temp = super.delete(inInfo, "ZMCLReport_RiskRecord.delete","result");
		return query(_temp);
	}
	
	/**
	 * 检查是否添加重复的隐患项目存在
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-2  高强
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		
		List result = dao.query("ZMCLReport_RiskRecord.validate", params);
		if(result!=null && result.size() > 0) {
			inInfo.setStatus(-1);
			inInfo.setMsg("选择的隐患记录已经添加，请重新选择！");
			return true;
		}
		return false;
	}
	
	/**
	 * 
	 * <p>Function: getCivilizationProduceCount</p>
	 * <p>Description: </p>
	 * <p>return value:int</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-11-18上午08:51:05 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	public int getCivilizationProduceCount(String reportId) {
		int count = 0;
		try {
			Map<String, String> _argMap = new HashMap<String, String>();
			_argMap.put("fdDepartmentId", reportId);
			
			List result = dao.query("ZMCLReport_CivilizationProduce.query", _argMap);
			count = result.size();
		} catch (Exception e) {
			System.out.println("查询文明生产记录异常：" + e.getMessage());
		}
		return count;
	}
}
